<!--  线 + 柱混合图 -->
<template>
  <el-card>
    <template #header>
      <div class="title">本月巡检人次（单位：人次）</div>
    </template>

    <div :id="id" :class="className" :style="{ height, width }"></div>
  </el-card>
</template>

<script setup lang="ts">
import * as echarts from "echarts";

const props = defineProps({
  id: {
    type: String,
    default: "barChart",
  },
  className: {
    type: String,
    default: "",
  },
  width: {
    type: String,
    default: "200px",
    required: true,
  },
  height: {
    type: String,
    default: "160px",
    required: true,
  },
  byxjfxDate: {
    type: Array,
    default: () => {
      return [];
    },
  },
  byxjfxData: {
    type: Array,
    default: () => {
      return {
        jh: [],
        wc: [],
        xData: [],
      };
    },
  },
});
const byxjfxData = ref(props.byxjfxData);

const options = {
  grid: {
    left: "1%",
    right: "1%",
    bottom: "1%",
    top: "14%",
    containLabel: true,
  },
  tooltip: {
    trigger: "axis",
    axisPointer: {
      type: "cross",
      crossStyle: {
        color: "#999",
      },
    },
  },
  legend: {
    top: "0%",
    left: "center",
    textStyle: {
      fontSize: 12,
      color: "#000",
    },
  },
  xAxis: [
    {
      type: "category",
      data: byxjfxData.value.xData,
      axisPointer: {
        type: "shadow",
      },
    },
  ],
  yAxis: [
    {
      type: "value",
      axisLabel: {
        formatter: "{value} ",
      },
    },
  ],
  series: [
    {
      type: "line",
      name: "计划巡检",
      data: byxjfxData.value.jh,
      smooth: true, // 是否曲线
      barWidth: 20,
      areaStyle: {
        opacity: 0.6,
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: "#2196f387",
          },
          {
            offset: 1,
            color: "#2196f345",
          },
        ]),
      },
    },
    {
      type: "line",
      name: "完成巡检",
      data: byxjfxData.value.wc,
      smooth: true, // 是否曲线
      barWidth: 20,
      areaStyle: {
        opacity: 0.8,
        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
          {
            offset: 0,
            color: "#0064b4b0",
          },
          {
            offset: 1,
            color: "#2196f345",
          },
        ]),
      },
    },
  ],
};

const chart = ref<any>("");
onMounted(() => {
  // 图表初始化
  chart.value = markRaw(
    echarts.init(document.getElementById(props.id) as HTMLDivElement)
  );

  chart.value.setOption(options);

  // 大小自适应
  window.addEventListener("resize", () => {
    chart.value.resize();
  });
});

onActivated(() => {
  if (chart.value) {
    chart.value.resize();
  }
});
</script>
<style lang="scss" scoped>
.title {
  display: flex;
  justify-content: space-between;

  .download {
    cursor: pointer;

    &:hover {
      color: #32bebd;
    }
  }
}
</style>
